console.log("虚拟数据已加载")

let fake = {
    self: null,
    account: [{
            username: 'admin',
            password: 'admin',
            user: {
                info: {
                    id: "1",
                    nick: "admin",
                    avatar: require(`logo`),
                    sex: "男",
                    desc: "该用户什么也没留下",
                    creatTime: "2022-4-12 22:07",
                },
                line: [{
                        content: "关注",
                        list: ['2'],
                    },
                    {
                        content: "粉丝",
                        list: ['2', '3'],
                    },
                    {
                        content: "收藏",
                        list: ['1'],
                    },
                    {
                        content: "文章",
                        list: ['2'],
                    },
                ],
                set: {
                    showFocus: true,
                    showFans: true,
                    showLikes: true,
                    showViews: true,
                    showArticle: true,
                },
                isAdmin: true,
                msgBox: ['1', '2'],
            },
        },
        {
            username: '橘子海',
            password: 'admin',
            user: {
                info: {
                    id: "2",
                    nick: "橘子海",
                    avatar: require(`img_1`),
                    sex: "男",
                    desc: "该用户什么也没留下",
                    creatTime: "2022-4-12 22:07",
                },
                line: [{
                        content: "关注",
                        list: ['1'],
                    },
                    {
                        content: "粉丝",
                        list: ['1', '4', '5', '6'],
                    },
                    {
                        content: "收藏",
                        list: ['2'],
                    },
                    {
                        content: "文章",
                        list: ['1'],
                    },
                ],
                set: {
                    showFocus: false,
                    showFans: true,
                    showLikes: true,
                    showViews: false,
                    showArticle: true,
                },
                isAdmin: false,
                msgBox: [],
            },
        },
        {
            username: '热狗',
            password: 'admin',
            user: {
                info: {
                    id: "3",
                    nick: "热狗",
                    avatar: require(`img_2`),
                    sex: "男",
                    desc: "该用户什么也没留下",
                    creatTime: "2022-4-12 22:07",
                },
                line: [{
                        content: "关注",
                        list: ['1'],
                    },
                    {
                        content: "粉丝",
                        list: [],
                    },
                    {
                        content: "收藏",
                        list: [],
                    },
                    {
                        content: "文章",
                        list: [],
                    },
                ],
                set: {
                    showFocus: true,
                    showFans: true,
                    showLikes: true,
                    showViews: true,
                    showArticle: true,
                },
                isAdmin: false,
                msgBox: [],
            },
        },
        {
            username: '阿城',
            password: 'admin',
            user: {
                info: {
                    id: "5",
                    nick: "阿城",
                    avatar: require(`img_3`),
                    sex: "男",
                    desc: "该用户什么也没留下",
                    creatTime: "2022-4-12 22:07",
                },
                line: [{
                        content: "关注",
                        list: ['2'],
                    },
                    {
                        content: "粉丝",
                        list: [],
                    },
                    {
                        content: "收藏",
                        list: [],
                    },
                    {
                        content: "文章",
                        list: [],
                    },
                ],
                set: {
                    showFocus: true,
                    showFans: true,
                    showLikes: true,
                    showViews: true,
                    showArticle: true,
                },
                isAdmin: false,
                msgBox: [],
            },
        },
        {
            username: '秋天',
            password: 'admin',
            user: {
                info: {
                    id: "6",
                    nick: "秋天",
                    avatar: require(`img_4`),
                    sex: "男",
                    desc: "该用户什么也没留下",
                    creatTime: "2022-4-12 22:07",
                },
                line: [{
                        content: "关注",
                        list: ['2'],
                    },
                    {
                        content: "粉丝",
                        list: [],
                    },
                    {
                        content: "收藏",
                        list: [],
                    },
                    {
                        content: "文章",
                        list: [],
                    },
                ],
                set: {
                    showFocus: true,
                    showFans: true,
                    showLikes: true,
                    showViews: true,
                    showArticle: true,
                },
                isAdmin: false,
                msgBox: [],
            },
        },
        {
            username: 'uuuu',
            password: 'admin',
            user: {
                info: {
                    id: "7",
                    nick: "uuuu",
                    avatar: require(`logo`),
                    sex: "男",
                    desc: "该用户什么也没留下",
                    creatTime: "2022-4-12 22:07",
                },
                line: [{
                        content: "关注",
                        list: [],
                    },
                    {
                        content: "粉丝",
                        list: [],
                    },
                    {
                        content: "收藏",
                        list: [],
                    },
                    {
                        content: "文章",
                        list: [],
                    },
                ],
                set: {
                    showFocus: true,
                    showFans: true,
                    showLikes: true,
                    showViews: true,
                    showArticle: true,
                },
                isAdmin: false,
                msgBox: [],
            },
        },
        {
            username: '小米粥',
            password: 'admin',
            user: {
                info: {
                    id: "4",
                    nick: "小米粥",
                    avatar: require(`img_4`),
                    sex: "男",
                    desc: "该用户什么也没留下",
                    creatTime: "2022-4-12 22:07",
                },
                line: [{
                        content: "关注",
                        list: ['2'],
                    },
                    {
                        content: "粉丝",
                        list: [],
                    },
                    {
                        content: "收藏",
                        list: [],
                    },
                    {
                        content: "文章",
                        list: [],
                    },
                ],
                set: {
                    showFocus: true,
                    showFans: true,
                    showLikes: true,
                    showViews: true,
                    showArticle: true,
                },
                isAdmin: false,
                msgBox: [],
            },
        },
    ],
    article: {
        list: [{
            writerId: "2",
            id: "1",
            title: "我是橘子海，我来啦",
            tags: [{
                    content: "杂谈",
                    type: "danger"
                },
                {
                    content: "游戏",
                    type: "success"
                },
            ],
            imgs: [],
            content: "大家快来互关呀",
            time: "2022-4-12 22:07",
            comments: 11,
            commentsList: ["1", "2"],
            views: 5,
            viewList: ['1', '2', '4', '5', '7'],
            likes: 1,
            likesList: ['1'],
            collection: 1,
            collectionList: ['1'],
        }, {
            writerId: "1",
            id: "2",
            title: "大家好，我是admin",
            tags: [{
                    content: "美食",
                    type: "danger"
                },
                {
                    content: "旅游",
                    type: "success"
                },
            ],
            imgs: [],
            content: "希望各位遵守社区规则",
            time: "2022-5-3 22:07",
            comments: 11,
            commentsList: ["1", "2"],
            views: 5,
            viewList: ['1', '2', '4', '5', '7'],
            likes: 1,
            likesList: ['2'],
            collection: 1,
            collectionList: ['2'],
        }, ],
        commentsList: [{
                id: "1",
                content: "这是一条评论",
                likes: 30,
                time: "2022-4-28 15:48",
                from: "1",
                to: null,
                children: [{
                        id: "11",
                        content: "这是一条回复",
                        likes: 1,
                        time: "2022-4-28 15:48",
                        from: '4',
                        to: null,
                        children: null,
                    },
                    {
                        id: "12",
                        content: "这是一条指定回复",
                        likes: 0,
                        time: "2022-4-28 15:48",
                        from: '5',
                        to: '4',
                        children: null,
                    },
                    {
                        id: "13",
                        content: "这是id为 13 的回复",
                        likes: 1,
                        time: "2022-4-28 15:48",
                        from: '5',
                        to: null,
                        children: null,
                    },
                    {
                        id: "14",
                        content: "这是id为 14 回复",
                        likes: 1,
                        time: "2022-4-28 15:48",
                        from: '4',
                        to: null,
                        children: null,
                    },
                    {
                        id: "15",
                        content: "这是id为 15 回复",
                        likes: 1,
                        time: "2022-4-28 15:48",
                        from: '5',
                        to: '1',
                        children: null,
                    },
                ],
            },
            {
                id: "2",
                content: "楼主的评论",
                likes: 30,
                time: "2022-4-29 15:01",
                from: '2',
                to: null,
                children: [{
                        id: "21",
                        content: "回复楼主的评论",
                        likes: 3,
                        time: "2022-4-29 15:51",
                        from: '7',
                        to: null,
                        children: null,
                    },
                    {
                        id: "22",
                        content: "来自楼主的回复",
                        likes: 3,
                        time: "2022-4-29 15:51",
                        from: '1',
                        to: '7',
                        children: null,
                    },
                    {
                        id: "23",
                        content: "来自楼主的第二条回复",
                        likes: 3,
                        time: "2022-4-29 15:51",
                        from: '1',
                        to: '7',
                        children: null,
                    },
                    {
                        id: "24",
                        content: "来自楼主的第三条回复",
                        likes: 3,
                        time: "2022-4-29 15:51",
                        from: '1',
                        to: '7',
                        children: null,
                    },
                ],
            },
        ],
    },
    msgList: [{
            id: '1',
            type: 'notice',
            from: '1',
            content: '欢迎加入社区',
            time: '2022-5-3 21:37',
        },
        {
            id: '2',
            from: '1',
            type: 'msg',
            content: '你好，新朋友',
            time: '2022-5-3 21:37',
        }
    ],
    methods: {
        getDateFormat: () => {
            let date = new Date()
            let year = date.getFullYear();
            let month = (date.getMonth() + 1).toString();
            let day = (date.getDate()).toString();
            let hours = (date.getHours()).toString();
            let minutes = (date.getMinutes()).toString();
            if (month.length == 1) {
                month = "0" + month;
            }
            if (day.length == 1) {
                day = "0" + day;
            }
            if (hours.length == 1) {
                hours = "0" + hours;
            }
            if (minutes.length == 1) {
                minutes = "0" + minutes;
            }
            return year + "-" + month + "-" + day + " " + hours + ":" + minutes;
        },
        addComment: (writerId, articleId, content) => {
            let commentId = new Date().getTime() + ''
            fake.methods.getArticleById(articleId).commentsList.push(commentId)
            fake.methods.getArticleById(articleId).comments += 1
            fake.article.commentsList.push({
                id: commentId,
                content: content,
                likes: 0,
                time: fake.methods.getDateFormat(),
                from: writerId,
                to: null,
            })
        },
        getUserInfo: (id) => {
            if (id !== null) {
                return fake.methods.getUser(id).info;
            } else {
                console.error('id 不能为 null')
                return {}
            }
        },
        getUser: (id) => {
            let capacity = null;
            fake.account.forEach(value => {
                if (value.user.info.id === id) {
                    capacity = value.user
                }
            })
            return capacity;
        },
        getArticleByWriter: (writerId) => {
            let capacity = []
            fake.article.list.forEach(value => {
                if (value.writerId === writerId) {
                    capacity.push(value)
                }
            });
            return capacity
        },
        getArticleById: (articleId) => {
            let capacity = null;
            fake.article.list.forEach(value => {
                if (value.id === articleId) {
                    capacity = value
                }
            });
            return capacity
        },
        createArticle: (writerId, title, content, tags, imgs) => {
            let id = new Date().getTime() + ''
            fake.article.list.push({
                writerId: writerId,
                id: id,
                title: title,
                tags: tags,
                imgs: imgs,
                content: content,
                time: fake.methods.getDateFormat(),
                comments: 0,
                commentsList: [],
                views: 0,
                viewList: [],
                likes: 0,
                likesList: [],
                collection: 0,
                collectionList: [],
            })
            fake.account.forEach(value => {
                if (value.user.info.id === writerId) {
                    value.user.line.forEach(lineValue => {
                        if (lineValue.content === '文章') {
                            lineValue.list.push(id)
                        }
                    })
                }
            })
        },
        addComment: (writerId, articleId, content) => {
            let commentId = new Date().getTime() + ''
            fake.methods.getArticleById(articleId).commentsList.push(commentId)
            fake.methods.getArticleById(articleId).comments += 1
            fake.article.commentsList.push({
                id: commentId,
                content: content,
                likes: 0,
                time: fake.methods.getDateFormat(),
                from: writerId,
                to: null,
                children: [],
            })
        },
        addReplay: (writerId, articleId, commentId, content, to) => {
            let replayId = new Date().getTime() + ''
            fake.methods.getArticleById(articleId).comments += 1
            fake.article.commentsList.forEach(value => {
                if (value.id === commentId) {
                    value.children.push({
                        id: replayId,
                        content: content,
                        likes: 0,
                        time: fake.methods.getDateFormat(),
                        from: writerId,
                        to: to,
                        children: null,
                    })
                }
            })
        },
        getComments: (articleId) => {
            let capacity = [];
            fake.methods.getArticleById(articleId).commentsList.forEach(value => {
                fake.article.commentsList.forEach(list => {
                    if (value === list.id) {
                        capacity.push(list)
                    }
                })
            })
            return capacity;
        },
        operate: (articleId, from, type) => {
            fake.article.list.forEach(value => {
                if (value.id === articleId) {
                    if (type === '点赞') {
                        let index = value.likesList.indexOf(from)
                        if (index === -1) {
                            value.likes += 1
                            value.likesList.push(from)
                        } else {
                            value.likes -= 1
                            value.likesList.splice(index, 1)
                        }
                    } else if (type === '收藏') {
                        let index = value.collectionList.indexOf(from)
                        if (index === -1) {
                            fake.account.forEach(value => {
                                if (value.user.info.id === from) {
                                    value.user.line.forEach(collectionValue => {
                                        if (collectionValue.content === '收藏') {
                                            collectionValue.list.push(articleId)
                                        }
                                    })
                                }
                            })
                            value.collection += 1
                            value.collectionList.push(from)
                        } else {
                            fake.account.forEach(value => {
                                if (value.user.info.id === from) {
                                    value.user.line.forEach(collectionValue => {
                                        if (collectionValue.content === '收藏') {
                                            let position = collectionValue.list.indexOf(articleId)
                                            collectionValue.list.splice(position, 1)
                                        }
                                    })
                                }
                            })
                            value.collection -= 1
                            value.collectionList.splice(index, 1)
                        }
                    } else if (type === '浏览') {
                        let index = value.viewList.indexOf(from)
                        if (index === -1) {
                            value.views += 1
                            value.viewList.push(from)
                        }
                    } else {
                        console.error(type + ' 是不支持的')
                    }
                }
            });
        },
        getOperateStatus: (articleId, from, type) => {
            let status = false
            fake.article.list.forEach(value => {
                if (value.id === articleId) {
                    if (type === '点赞') {
                        status = value.likesList.indexOf(from) !== -1
                    } else if (type === '收藏') {
                        status = value.collectionList.indexOf(from) !== -1
                    } else if (type === '浏览') {
                        status = value.viewList.indexOf(from) !== -1
                    } else {
                        console.error(type + ' 是不支持的')
                    }
                }
            });
            return status
        },
        deleteArticle: (articleId) => {
            let user = fake.methods.getUser(fake.methods.getArticleById(articleId).writerId)
            user.line.forEach((value) => {
                if (value.content === '文章') {
                    value.list.splice(value.list.indexOf(articleId), 1)
                }
            })
            fake.account.forEach(value => {
                value.user.line.forEach(lineValue => {
                    if (lineValue.content === '收藏') {
                        let index = lineValue.list.indexOf(articleId)
                        if (index !== -1) {
                            lineValue.list.splice(index, 1)
                        }
                    }
                })
            })
            fake.article.list.forEach((value, index) => {
                if (value.id === articleId) {
                    fake.article.list.splice(index, 1)
                }
            });
        },
        sendMsg: (from, to, content, type) => {
            let id = new Date().getTime() + ''
            fake.account.forEach(value => {
                if (value.user.info.id === to) {
                    value.user.msgBox.push(id)
                }
            })
            fake.msgList.push({
                id: id,
                type: type,
                from: from,
                content: content,
                time: fake.methods.getDateFormat(),
            })
        },
        noticeAll: (content) => {
            let id = new Date().getTime() + ''
            fake.account.forEach(value => {
                value.user.msgBox.push(id)
            })
            fake.msgList.push({
                id: id,
                type: 'notice',
                from: null,
                content: content,
                time: fake.methods.getDateFormat(),
            })
        },
        deleteMsg: (userId, id) => {
            fake.account.forEach(value => {
                if (value.user.info.id === userId) {
                    let index = value.user.msgBox.indexOf(id)
                    value.user.msgBox.splice(index, 1)
                }
            })
            fake.msgList.forEach((value, index) => {
                if (value.id === id) {
                    fake.msgList.splice(index, 1)
                }
            })
        },
        getMsg: (userId) => {
            let capacity = []
            fake.methods.getUser(userId).msgBox.forEach(id => {
                fake.msgList.forEach(msg => {
                    if (msg.id === id) {
                        capacity.push(msg)
                    }
                })
            })
            return capacity;
        },
        focusWith: (isAdd, from, to) => {
            fake.account.forEach(value => {
                if (value.user.info.id === from) {
                    value.user.line.forEach(line => {
                        if (line.content === '关注') {
                            if (isAdd) {
                                line.list.push(to)
                            } else {
                                let position = line.list.indexOf(to)
                                line.list.splice(position, 1)
                            }
                        }
                    })
                }
                if (value.user.info.id === to) {
                    value.user.line.forEach(line => {
                        if (line.content === '粉丝') {
                            if (isAdd) {
                                line.list.push(from)
                            } else {
                                let position = line.list.indexOf(from)
                                line.list.splice(position, 1)
                            }
                        }
                    })
                }
            })
            if (isAdd) {
                fake.methods.sendMsg(null, to, fake.methods.getUserInfo(from).nick + ' 关注了你', 'notice')
            }
        },
    }
}

let loading = (() => {
    fake.methods.createArticle('3',
            '关于Vue中路由守卫全方位解析',
            '导航守卫的作用：vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航。\r项目中应用场景举例：路由页面跳转时候进行登陆验证；路由跳转判断；\n有多种机会植入路由导航过程中：全局路由, 单个路由独享的, 或者组件级的。\n全局守卫包括：router.beforeEach（是全局前置守卫）、router.beforeResolve（是全局解析守卫）、router.afterEach（是全局后置钩子）',
            [{
                content: '学习',
                type: "success",
            }],
            []),
        setTimeout(() => {
            fake.methods.createArticle('4',
                '单例模式之双重判断锁',
                '安全且在多线程情况下能保持高性能，第一个if判断避免了其他无用线程竞争锁来造成性能浪费，第二个if判断能拦截除第一个获得对象锁线程以外的线程。如果不加第二次判空，我们考虑下线程A，线程B都阻塞在了获取锁的步骤上，其中A获得锁---实例化了对象----释放锁，之后B---获得锁---实例化对象，此时违反了我们单例模式的初衷',
                [{
                    content: '学习',
                    type: "success",
                }],
                [])
        }, 500)
        setTimeout(() => {
            fake.methods.createArticle('5',
                'SpringBoot2.0集成WebSocket，实现后台向前端推送信息',
                'WebSocket协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端。' + 
                ' <p> </p> ' +
                '<h5><strong>为什么需要 WebSocket？</strong></h5>' + 
                ' <p> </p> '+ 
                '因为 HTTP 协议有一个缺陷：通信只能由客户端发起，HTTP 协议做不到服务器主动向客户端推送信息,举例来说，我们想要查询当前的排队情况，只能是页面轮询向服务器发出请求，服务器返回查询结果。轮询的效率低，非常浪费资源（因为必须不停连接，或者 HTTP 连接始终打开）。因此WebSocket 就是这样发明的',
                [{
                    content: '学习',
                    type: "success",
                }],
                [])
        }, 600)
})()
export {
    fake
}